/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.wrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xy.entity.CertificateApplyEntity;
import com.xy.entity.CertificateTempEntity;
import com.xy.entity.CertificateTypeEntity;
import com.xy.vo.CertificateTempVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.SysCache;
import org.springblade.system.cache.UserCache;
import org.springblade.system.entity.Dept;
import org.springblade.system.entity.User;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户-证类型中间表 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2023-07-25
 */
public class CertificateTempWrapper extends BaseEntityWrapper<CertificateTempEntity, CertificateTempVO>  {

	public static CertificateTempWrapper build() {
		return new CertificateTempWrapper();
 	}

	@Override
	public CertificateTempVO entityVO(CertificateTempEntity certificateTemp) {
		CertificateTempVO certificateTempVO = Objects.requireNonNull(BeanUtil.copy(certificateTemp, CertificateTempVO.class));
		return certificateTempVO;
	}
	public CertificateTempVO entityVO1(CertificateTempEntity certificateTemp, Map<String, CertificateTypeEntity> typeMap,Map<String, List<CertificateApplyEntity>>  countMap  ) {
		if(Func.isEmpty(certificateTemp)){
			return  new CertificateTempVO();
		}
		CertificateTempVO certificateTempVO = Objects.requireNonNull(BeanUtil.copy(certificateTemp, CertificateTempVO.class));
		certificateTempVO.setUsaValu(0);
		User user = UserCache.getUser(certificateTemp.getUserId());
 		if(Func.isNotEmpty(user)){
			String name = user.getName();
			if(Func.isNotEmpty(name)){
				certificateTempVO.setUserName(name);
			}
			if (Func.isNotEmpty(user.getRealName())) {
				certificateTempVO.setRealName(user.getRealName());
			}
 		}

		Dept dept = SysCache.getDept(certificateTemp.getCreateDept());
		if(Func.isNotEmpty(dept)){
			String name = dept.getDeptName();
			if(Func.isNotEmpty(name)){
				certificateTempVO.setCreateDeptName(name);
			}
		}
		if(Func.isNotEmpty(typeMap)){
			CertificateTypeEntity certificateTypeEntity = typeMap.get(Func.toStr(certificateTemp.getCertificateTypeId()));
			if(Func.isNotEmpty(certificateTypeEntity)){
				certificateTempVO.setType(certificateTypeEntity);
			}
		}
		List<CertificateApplyEntity> certificateApplyEntities = countMap.get(Func.toStr(certificateTemp.getCertificateTypeId()));
		if (Func.isNotEmpty(certificateApplyEntities) && !Func.equals("null",certificateApplyEntities)) {
			Map<String, List<CertificateApplyEntity>> collectByUserId = certificateApplyEntities.stream().collect(Collectors.groupingBy(t->Func.toStr(t.getCreateUser())));
			if(Func.isNotEmpty(collectByUserId)){
				List<CertificateApplyEntity> certificateApplyEntities1 = collectByUserId.get(Func.toStr(certificateTemp.getUserId()));
				if (Func.isNotEmpty(certificateApplyEntities1) && !Func.equals("null",certificateApplyEntities1)) {
					certificateTempVO.setUsaValu(certificateApplyEntities1.size());
				}
			}

		}
		return certificateTempVO;
	}

	public List<CertificateTempVO> listVO1(List<CertificateTempEntity> list,Map<String, CertificateTypeEntity> typeMap,Map<String, List<CertificateApplyEntity>>  countMap ) {
		return  list.stream().map(m->entityVO1(m,typeMap,countMap)).collect(Collectors.toList());
	}

	public IPage<CertificateTempVO> pageVO1(IPage<CertificateTempEntity> pages,Map<String, CertificateTypeEntity> typeMap,	Map<String, List<CertificateApplyEntity>>  countMap) {
		List<CertificateTempVO> records = listVO1(pages.getRecords(),typeMap ,countMap);
		IPage<CertificateTempVO> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
		pageVo.setRecords(records);
		return pageVo;
	}

}
